import {observable, action} from "mobx";
import request from "../utils/request";
import * as api from "../api/api";
import {LoadHoc, loading} from '../hoc/loadHoc';

// 建企商学院
export default class BusinessSchool extends LoadHoc {
  /**
   * 课程类别信息
   */
  @observable allCategories: api.YNJWTBusinessSchoolVOCourseCategoriesVO = {};

  @loading @action
  async getAllCategories(params: {
    callback?: (allCategories: api.YNJWTBusinessSchoolVOCourseCategoriesVO) => void;
  }) {
    const {callback} = params;
    const allCategories = await request(api.AppbusinessSchoolApiFetchParamCreactor.appbusinessSchoolGetAllCategoriesAsync({}));
    this.allCategories = allCategories;
    callback && callback(allCategories);
  }

  /**
   * 所有课程信息
   */
  @observable allCourseInformation: api.YNJWTBusinessSchoolVOCourseInformationVO = {};

  @loading @action
  async getAllCourseInformation(params: {
    cateGoryId: number,
    pageIndex,
    pageSize,
    callback?: (allCategories: api.YNJWTBusinessSchoolVOCourseInformationVO) => void;
  }) {
    const {cateGoryId, pageIndex, pageSize, callback} = params;
    const allCourseInformation = await request(api.AppbusinessSchoolApiFetchParamCreactor.appbusinessSchoolGetAallCourseInformationAsync({
      formCateGoryId: cateGoryId,
      formSkipCount: (pageIndex-1)*pageSize,
      formMaxResultCount: pageSize,
    }));
    this.allCourseInformation = allCourseInformation;
    callback && callback(allCourseInformation);
  }

  /**
   * 所有课程详情
   */
  @observable courseInformation: api.YNJWTBusinessSchoolVOCourseInformationVO = {};

  @loading @action
  async getCourseInformation(params: {
    id,
    callback?: (allCategories: api.YNJWTBusinessSchoolVOCourseInformationVO) => void;
  }) {
    const {id, callback} = params;
    const courseInformation = await request(api.AppbusinessSchoolApiFetchParamCreactor.appbusinessSchoolGetCourseInformationInfoAsync({
      formId: id
    }));
    this.courseInformation = courseInformation;
    callback && callback(courseInformation);
  }

  /**
   * 所有课程章节信息
   */
  @observable courseChapterInfo: api.YNJWTBusinessSchoolVOCourseChaptersVO = {};

  @loading @action
  async getCourseChapterInfo(params: {
    formType,
    courseId,
    pageIndex,
    pageSize,
    callback?: (allCategories: api.YNJWTBusinessSchoolVOCourseChaptersVO) => void;
  }) {
    const {formType,courseId, pageIndex, pageSize, callback} = params;
    const courseChapterInfo = await request(api.AppbusinessSchoolApiFetchParamCreactor.appbusinessSchoolGetCourseChapterInfoAsync({
      formType,
      formCourseId: courseId,
      formSkipCount: (pageIndex-1)*pageSize,
      formMaxResultCount: pageSize,
    }));
    this.courseChapterInfo = courseChapterInfo;
    callback && callback(courseChapterInfo);
  }

  /**
   * 保存报名信息
   */
  @loading @action
  async saveApplyInfoAsync(params: {
    form,
    callback?: (res) => void;
  }) {
    const {form, callback} = params;
    const res = await request(api.AppbusinessSchoolApiFetchParamCreactor.appbusinessSchoolSaveApplyInfoAsync({
      form
    }));
    callback && callback(res);
  }

  /**
   * 购买课程
   */
  @loading @action
  async buyCourse(params: {
    form: {
      courseInfoId: number,
      courseChapterId: number
    },
    callback?: (res) => void;
  }) {
    const {form, callback} = params;
    const res = await request(api.AppbusinessSchoolApiFetchParamCreactor.appbusinessSchoolBuyCourseAsync({
      form
    }));
    callback && callback(res);
  }

  /**
   * 获取报名人数信息
   */
  @loading @action
  async getBusinessSchoolRegistCount(params: {
    callback?: (res: api.YNJWTBusinessSchoolVORegistCountVO) => void;
  }) {
    const { callback } = params;
    const businessSchoolRegistCount = await request(api.AppbusinessSchoolApiFetchParamCreactor.appbusinessSchoolGetBusinessSchoolRegistCountAsync({
    }));
    callback && callback(businessSchoolRegistCount);
  }
}
